<?php

namespace app\api\controller;

use app\api\model\Config;
use app\api\model\Coupon;
use app\api\model\LangClass AS LangClassModel;
use app\api\model\OauthOrderClass;
use app\api\model\Oauth as UserClassModel;

class LangClass extends Base
{
    /**
     * 获取班级介绍，通过语种ID和班级类型获取班级介绍
     * @param int $langId 语种id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function read($langId)
    {
        $type = input('type') ? input('type') : LangClassModel::CLASS_FIRST;
        if ($langId < 0) {
            return self::sendResult(100);
        }

        $typeArr = LangClassModel::attributes('type');
        if (!isset($typeArr[$type])) {
            return self::sendResult(102, [], '班级类型错误');
        }

        $model = LangClassModel::getOne($langId, $type);
        if (empty($model)) {
            return self::sendResult(122);
        }

        // 判断班级是否已经购买
        $hasBuy = OauthOrderClass::hasBuy($this->oauthInfo['id'], $model['classId']);
        $model['hasBuy'] = $hasBuy ? 1: 0;
        return self::sendResult(self::$okCode, $model);
    }

    /**
     * 购买班级信息
     * @param int $classId 班级ID
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function buyInfo($classId)
    {
        if ($classId < 0) {
            return self::sendResult(100);
        }

        // 用户授权表信息
        $oauthInfo = $this->oauthInfo;

        // 获取购买班级整个页面信息
        $model = LangClassModel::buyInfo($classId, $oauthInfo);

        return self::sendResult(self::$okCode, $model);
    }

    /**
     * 统一下单接口(小程序)
     * @param string $classIds 购买的班级id，多个用英文逗号隔开
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function prepay($classIds)
    {


        // 积分抵扣的金额
        $reduceMoney = input('reduceMoney') ? input('reduceMoney') : 0;
        // 优惠券id
        $couponId = input('couponId') ? input('couponId') : 0;
        // 客服id
        $customerId = input('customerId') ? input('customerId') : 0;
        // 分享键
        $shareKey = input('shareKey') ? input('shareKey') : '';
        //分享码 选填
        $code = input('code') ? input('code') : '';

        if (!is_array($classIds)) {
            return self::sendResult(102);
        }

        $classIds = array_unique(array_filter($classIds));
        if (empty($classIds) || count($classIds) <= 0) {
            return self::sendResult(102, [], '请选择要购买的班级');
        }

        // 判断课程有没购买过
        $has = OauthOrderClass::hasBuy($this->oauthInfo['id'], $classIds);
        if ($has) {
            return self::sendResult(127);
        }

        // 查找班级
        $class = LangClassModel::getClasses($classIds);
        if (empty($class)) {
            return self::sendResult(122);
        }

        // 可减的金额
        $money = 0;
        if ($reduceMoney > 0) {
            // 可兑换的金额
            $canReduce = Config::getReduceMoney($this->oauthInfo['integral']);
            if ($reduceMoney != $canReduce) {
                return self::sendResult(124);
            }
            $money += $canReduce;
        }

        // 查找优惠券
        if ($couponId > 0) {
            $coupon = Coupon::getOne($couponId);
            if (empty($coupon)) {
                return self::sendResult(125);
            }
            $money += $coupon['money'];
        }
//        //查找分享码
//        if (!empty($code)) {
//            $code = UserClassModel::getCode($code);
//            if (empty($code)) {
//                unset($code);
//            } else {
//                $code = $code['code'];
//            }
//        }
        // 处理班级数据
        $classData = LangClassModel::classData($class);

        $realMoney = $classData['sum'] - $money;

        // 一次性购买多个班级，可享受优惠价格，每个班级优惠100元
        $classNum = count($class);
        if ($classNum > 1) {
            $after = $realMoney - ($classNum * 100);
            $realMoney = $after > 0 ? $after : 0;
        }

        $openid = $this->oauthInfo['openid'];
        $return = LangClassModel::prepay($openid, $classData, $realMoney, $reduceMoney, $couponId, $customerId,$code, $shareKey);

        $code = ($return['success'] === true) ? self::$okCode : 121;
        return self::sendResult($code, $return['data']);
    }
    
    
    
    
    /**
     * 统一下单接口(app支付)
     * @param string $classIds 购买的班级id，多个用英文逗号隔开
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function prepayApp($classIds)
    {
        $classIds = [23,24,25];
        // 积分抵扣的金额
        $reduceMoney = input('reduceMoney') ? input('reduceMoney') : 0;
        // 优惠券id
        $couponId = input('couponId') ? input('couponId') : 0;
        // 客服id
        $customerId = input('customerId') ? input('customerId') : 0;
        // 分享键
        $shareKey = input('shareKey') ? input('shareKey') : '';
        // 支付方式
        $payType  = input('payType');
        //分享码 选填
        $code = input('code') ? input('code') : '';
        
        if (empty($payType)) {
            return self::sendResult(102, [], '支付方式不能为空');
        }

        if (!is_array($classIds)) {
            return self::sendResult(102);
        }

        $classIds = array_unique(array_filter($classIds));
        if (empty($classIds) || count($classIds) <= 0) {
            return self::sendResult(102, [], '请选择要购买的班级');
        }
    
        // 判断课程有没购买过
        $has = OauthOrderClass::hasBuy($this->oauthInfo['id'], $classIds);
        if ($has) {
            return self::sendResult(127);
        }
    
        // 查找班级
        $class = LangClassModel::getClasses($classIds);
        if (empty($class)) {
            return self::sendResult(122);
        }
    
        // 可减的金额
        $money = 0;
        if ($reduceMoney > 0) {
            // 可兑换的金额
            $canReduce = Config::getReduceMoney($this->oauthInfo['integral']);
            if ($reduceMoney != $canReduce) {
                return self::sendResult(124);
            }
            $money += $canReduce;
        }
    
        // 查找优惠券
        if ($couponId > 0) {
            $coupon = Coupon::getOne($couponId);
            if (empty($coupon)) {
                return self::sendResult(125);
            }
            $money += $coupon['money'];
        }

        // 处理班级数据
        $classData = LangClassModel::classData($class);
    
        $realMoney = $classData['sum'] - $money;
    
        // 一次性购买多个班级，可享受优惠价格，每个班级优惠100元
        $classNum = count($class);
        if ($classNum > 1) {
            $after = $realMoney - ($classNum * 100);
            $realMoney = $after > 0 ? $after : 0;
        }
    
        $uid = $this->oauthInfo['id'];
        $return = LangClassMod1el::prepayApp($uid, $classData, $realMoney, $reduceMoney, $couponId, $customerId, $shareKey,$payType,$code);
    
        $code = ($return['success'] === true) ? self::$okCode : 121;
        return self::sendResult($code, $return['data']);
    }
}
